package com.itcm.manage.controller;

import com.itcm.manage.common.R;
import com.itcm.manage.common.ResultCodeEnum;
import com.itcm.manage.entity.SysUser;
import com.itcm.manage.entity.dto.LoginUser;
import com.itcm.manage.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author sky
 * @Site cmtianxie163.com 2020/6/18 10:13
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class SysUserController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "用户注册")
    @PostMapping("register")
    public R regist(@RequestBody SysUser user){
        SysUser register = sysUserService.register(user);
        if(register == null){
            return R.error();
        }else {
            return R.ok().data("data",register);
        }
    }

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody LoginUser loginUser){
        String token = sysUserService.login(loginUser.getUsername(), loginUser.getPassword());
        if(token == null){
            return R.setResult(ResultCodeEnum.VALIDATE_FAILED).message("用户名密码错误");
        }
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("tokenHeader",tokenHeader);
        tokenMap.put("token", tokenHead+' '+token);
        return R.ok().data("data",tokenMap);
    }
}
